package com.bookm.bookm.service.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanListHandler;
import com.bookm.bookm.entity.College;
import com.bookm.bookm.entity.Professional;
import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.CollegeService;
import com.bookm.bookm.utils.ResultUtil;

import java.sql.SQLException;
import java.util.List;

public class CollegeServiceImpl implements CollegeService {
    @Override
    public Result getCollegeList() {
        List<College> college = null;
        try {
            // 查询所有学院的信息并转化为 List<College> 对象
             college = Db.use().find(Entity.create("college"), BeanListHandler.create(College.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(college == null){
            return ResultUtil.failure();
        }
        return ResultUtil.success(college);
    }

    @Override
    public Result getProfList(Integer cid) {
        List<Professional> data = null;
        try {
            // 这里根据学院id获取对应学院下的专业的信息
            if(cid == -1){
                data = Db.use().find(Entity.create("professional"), BeanListHandler.create(Professional.class));
            }else{
                data = Db.use().find(Entity.create("professional").set("cid", cid), BeanListHandler.create(Professional.class));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(data == null){
            return ResultUtil.failure();
        }
        return ResultUtil.success(data);
    }
}
